set more off
clear all

*Defining the working folder

global main "insert your directory"

cd "$main"


////////////////////////////////////////////////////////////////
//---------------Data construction for figures 1 y 2------------
////////////////////////////////////////////////////////////////

*The files named "chl_YYYY" correspond to the data of the Latinobarómetro survey of the year YYYY that consider only the data from Chile.

*All databases are available on the latinobarometro corporation website (https://www.latinobarometro.org/).

//-----------------
// 1995 Survey
//-----------------

clear

*Load 1995 database

use "chl_1995"

*President 

gen president="Frei Ruiz-Ragle"

*Year

gen year=1995

*Government approval

generate approval = .
replace approval = 1 if p33==152001 | p33==152002 | p33==152003 | p33==152004 
replace approval = 0 if p33==152005 | p33==152006 | p33==152007 | p33==152010 | p33==152011 |  p33==152024 |  p33==94 |  p33==96 | p33==-2 | p33==-1

*Current sociotropic variable

generate socio_current = .
replace  socio_current = 0 if p1==4 | p1==5
replace  socio_current = 1 if p1==3
replace  socio_current = 2 if p1==1 | p1==2

*Current egotropic variable

generate ego_current = .
replace  ego_current = 0 if p4==4 | p4==5
replace  ego_current = 1 if p4==3
replace  ego_current = 2 if p4==1 | p4==2

keep year president approval socio_current ego_current wt

*Save 1995 database

save "ap_lb1995", replace

//-----------------
// 1996 Survey
//-----------------

clear

*Load 1996 database

use "chl_1996"

*President 

gen president="Frei Ruiz-Ragle"

*Year

gen year=1996

*Government approval

generate approval = .
replace approval = 1 if p40==152001 | p40==152002 | p40==152003 | p40==152004 
replace approval = 0 if p40==152005 | p40==152006 | p40==152007 | p40==152010 | p40==152011 |  p40==94 | p40==96 | p40==-2 | p40==-1

*Current sociotropic variable

generate socio_current = .
replace  socio_current = 0 if p1==4 | p1==5
replace  socio_current = 1 if p1==3
replace  socio_current = 2 if p1==1 | p1==2

*Current egotropic variable

generate ego_current = .
replace  ego_current = 0 if p4==4 | p4==5
replace  ego_current = 1 if p4==3
replace  ego_current = 2 if p4==1 | p4==2

keep year president approval socio_current ego_current wt


*Save 1996 database

save "ap_lb1996", replace

//-----------------
// 1997 Survey
//-----------------

clear

*Load 1997 database

use "chl_1997"

*President 

gen president="Frei Ruiz-Ragle"

*Year

gen year=1997

*Government approval

generate approval = .
replace approval = 1 if sp58==152001 | sp58==152002 | sp58==152003 
replace approval = 0 if sp58==152005 | sp58==152006 | sp58==152007 | sp58==152010 | sp58==152011 |  sp58==94 | sp58==-1 | sp58==-2 

*Current sociotropic variable

generate socio_current = .
replace  socio_current = 0 if sp1==4 | sp1==5
replace  socio_current = 1 if sp1==3
replace  socio_current = 2 if sp1==1 | sp1==2

*Current egotropic variable

generate ego_current = .
replace  ego_current = 0 if sp4==4 | sp4==5
replace  ego_current = 1 if sp4==3
replace  ego_current = 2 if sp4==1 | sp4==2

keep year president approval socio_current ego_current wt

*Save 1997 database

save "ap_lb1997", replace

//-----------------
// 1998 Survey
//-----------------

clear

*Load 1998 database

use "chl_1998"

*President 

gen president="Frei Ruiz-Ragle"

*Year

gen year=1998

*Government approval 

generate approval = .
replace approval = 1 if sp53==152001 | sp53==152002 | sp53==152003 | sp53==152004 
replace approval = 0 if sp53==152005 | sp53==152006 | sp53==152007 | sp53==152010 | sp53==152011 |  sp53==94 |  sp53==96 | sp53==-2 |  sp53==-1

*Current sociotropic variable

generate socio_current = .
replace  socio_current = 0 if sp1==4 | sp1==5
replace  socio_current = 1 if sp1==3
replace  socio_current = 2 if sp1==1 | sp1==2

*Current egotropic variable

generate ego_current = .
replace  ego_current = 0 if sp4==4 | sp4==5
replace  ego_current = 1 if sp4==3
replace  ego_current = 2 if sp4==1 | sp4==2

*Rename weight variable

rename pondera wt

keep year president approval socio_current ego_current wt

*Save 1998 database

save "ap_lb1998", replace

//-----------------
// 2001 Survey
//-----------------

clear

*Load 2001 database

use "chl_2001"

*President 

gen president="Lagos"

*Year

gen year=2001

*Government approval

generate approval = .
replace approval = 1 if p55st==152001 | p55st==152002 | p55st==152003 | p55st==152004 
replace approval = 0 if p55st==152005 | p55st==152006 | p55st==152007 | p55st==152010 | p55st==152011 |  p55st==94 |  p55st==95 | p55st==96 | p55st==-2 | p55st==-1


*Current sociotropic variable

generate socio_current = .
replace  socio_current = 0 if p1st==4 | p1st==5
replace  socio_current = 1 if p1st==3
replace  socio_current = 2 if p1st==1 | p1st==2

*Current egotropic variable

generate ego_current = .
replace  ego_current = 0 if p4st==4 | p4st==5
replace  ego_current = 1 if p4st==3
replace  ego_current = 2 if p4st==1 | p4st==2

keep year president approval socio_current ego_current wt

*Save 2001 database

save "ap_lb2001", replace


//-----------------
// 2002 Survey
//-----------------

*Load 2002 database

use "chl_2002"

*President 

gen president="Lagos"

*Year

gen year=2002

*Government approval

generate approval = .
replace approval = 1 if p18no2==1
replace approval = 0 if p18no2==2 | p18no2==-2

*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p2sta==4 | p2sta==5
replace  socio_current = 1 if p2sta==3
replace  socio_current = 2 if p2sta==1 | p2sta==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if p2std==4 | p2std==5
replace  ego_current = 1 if p2std==3
replace  ego_current = 2 if p2std==1 | p2std==2

keep year president approval socio_current ego_current wt

*Save 2002 database

save "ap_lb2002", replace

//-----------------
// 2003 Survey
//-----------------
clear

*Load 2003 database

use "chl_2003"

*President 

gen president="Lagos"

*Year

gen year=2003

*Government approval

generate approval = .
replace approval = 1 if p24st==1
replace approval = 0 if p24st==2 | p24st==-2

*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p1st==4 | p1st==5
replace  socio_current = 1 if p1st==3
replace  socio_current = 2 if p1st==1 | p1st==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if p4st==4 | p4st==5
replace  ego_current = 1 if p4st==3
replace  ego_current = 2 if p4st==1 | p4st==2

keep year president approval socio_current ego_current wt

*Save 2003 database

save "ap_lb2003", replace

//-----------------
// 2004 Survey
//-----------------
clear

*Load 2004 database

use "chl_2004"

*President 

gen president="Lagos"

*Year

gen year=2004

*Government approval

generate approval = .
replace approval = 1 if p46st==1
replace approval = 0 if p46st==2 | p46st==-2

*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p2st==4 | p2st==5
replace  socio_current = 1 if p2st==3
replace  socio_current = 2 if p2st==1 | p2st==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if p5st==4 | p5st==5
replace  ego_current = 1 if p5st==3
replace  ego_current = 2 if p5st==1 | p5st==2

keep year president approval socio_current ego_current wt

*Save 2004 database

save "ap_lb2004", replace

//-----------------
// 2005 Survey
//-----------------
clear

*Load 2005 database

use "chl_2005"

*President 

gen president="Lagos"

*Year

gen year=2005

*Government approval

generate approval = .
replace approval = 1 if p29st==1
replace approval = 0 if p29st==2 | p29st==-2

*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p2st==4 | p2st==5
replace  socio_current = 1 if p2st==3
replace  socio_current = 2 if p2st==1 | p2st==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if p5st==4 | p5st==5
replace  ego_current = 1 if p5st==3
replace  ego_current = 2 if p5st==1 | p5st==2

keep year president approval socio_current ego_current wt

*Save 2005 database

save "ap_lb2005", replace

//-----------------
// 2006 Survey
//-----------------
clear

*Load 2006 database

use "chl_2006"

*President 

gen president="Bachelet 1"

*Year

gen year=2006

*Government approval

generate approval = .
replace approval = 1 if p43st==1
replace approval = 0 if p43st==2 | p43st==-2

*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p2st==4 | p2st==5
replace  socio_current = 1 if p2st==3
replace  socio_current = 2 if p2st==1 | p2st==2

*Current egotropic variable

generate ego_current = .
replace  ego_current = 0 if p5st==4 | p5st==5
replace  ego_current = 1 if p5st==3
replace  ego_current = 2 if p5st==1 | p5st==2

keep year president approval socio_current ego_current wt

*Save 2006 database

save "ap_lb2006", replace


//-----------------
// 2008 Survey
//-----------------
clear

*Load 2008 database

use "chl_2008"

*President 

gen president="Bachelet 1"

*Year

gen year=2008

*Government approval

generate approval = .
replace approval = 1 if p32st_a==1
replace approval = 0 if p32st_a==2 | p32st_a==-2


*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p4st==4 | p4st==5
replace  socio_current = 1 if p4st==3
replace  socio_current = 2 if p4st==1 | p4st==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if p7st==4 | p7st==5
replace  ego_current = 1 if p7st==3
replace  ego_current = 2 if p7st==1 | p7st==2

keep year president approval socio_current ego_current wt

*Save 2008 database

save "ap_lb2008", replace

//-----------------
// 2009 Survey
//-----------------
clear

*Load 2009 database

use "chl_2009"

*President 

gen president="Bachelet 1"

*Year

gen year=2009

*Government approval

generate approval = .
replace approval = 1 if p68st_a==1
replace approval = 0 if p68st_a==2 | p68st_a==-2


*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if p3st_a==4 | p3st_a==5
replace  socio_current = 1 if p3st_a==3
replace  socio_current = 2 if p3st_a==1 | p3st_a==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if p6st==4 | p6st==5
replace  ego_current = 1 if p6st==3
replace  ego_current = 2 if p6st==1 | p6st==2

keep year president approval socio_current ego_current wt

*Save 2009 database

save "ap_lb2009", replace

//-----------------
// 2010 Survey
//-----------------
clear

*Load 2010 database

use "chl_2010"

*President 

gen president="Piñera 1"

*Year

gen year=2010

*Government approval

generate approval = .
replace approval = 1 if P59ST_A==1
replace approval = 0 if P59ST_A==2 | P59ST_A==-2


*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if P3ST_A==4 | P3ST_A==5
replace  socio_current = 1 if P3ST_A==3
replace  socio_current = 2 if P3ST_A==1 | P3ST_A==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if P6ST==4 | P6ST==5
replace  ego_current = 1 if P6ST==3
replace  ego_current = 2 if P6ST==1 | P6ST==2


keep year president approval socio_current ego_current wt

*Save 2010 database

save "ap_lb2010", replace

//-----------------
// 2011 Survey
//-----------------
clear

*Load 2011 database

use "chl_2011"

*President 

gen president="Piñera 1"

*Year

gen year=2011

*Government approval


generate approval = .
replace approval = 1 if P36ST==1
replace approval = 0 if P36ST==2 | P36ST==-1


*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if P3ST_A==4 | P3ST_A==5
replace  socio_current = 1 if P3ST_A==3
replace  socio_current = 2 if P3ST_A==1 | P3ST_A==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if P6ST==4 | P6ST==5
replace  ego_current = 1 if P6ST==3
replace  ego_current = 2 if P6ST==1 | P6ST==2


keep year president approval socio_current ego_current wt

*Save 2011 database

save "ap_lb2011", replace

//-----------------
// 2013 Survey
//-----------------
clear

*Load 2013 database

use "chl_2013"

*President 

gen president="Piñera 1"

*Year

gen year=2013

*Government approval


generate approval = .
replace approval = 1 if P38STGBS==1
replace approval = 0 if P38STGBS==2 | P38STGBS==-2


*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if P3STGBS==4 | P3STGBS==5
replace  socio_current = 1 if P3STGBS==3
replace  socio_current = 2 if P3STGBS==1 | P3STGBS==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if P6STGBS==4 | P6STGBS==5
replace  ego_current = 1 if P6STGBS==3
replace  ego_current = 2 if P6STGBS==1 | P6STGBS==2


keep year president approval socio_current ego_current wt

*Save 2013 database

save "ap_lb2013", replace

//-----------------
// 2015 Survey
//-----------------
clear

*Load 2015 database

use "chl_2015"

*President 

gen president="Bachelet 2"

*Year

gen year=2015

*Government approval


generate approval = .
replace approval = 1 if P48STGBS==1
replace approval = 0 if P48STGBS==2 | P48STGBS==-1


*Current sociotropic variable
generate socio_current = .
replace  socio_current = 0 if P3STGBS==4 | P3STGBS==5
replace  socio_current = 1 if P3STGBS==3
replace  socio_current = 2 if P3STGBS==1 | P3STGBS==2

*Current egotropic variable
generate ego_current = .
replace  ego_current = 0 if P6STGBS==4 | P6STGBS==5
replace  ego_current = 1 if P6STGBS==3
replace  ego_current = 2 if P6STGBS==1 | P6STGBS==2


keep year president approval socio_current ego_current wt

*Save 2015 database

save "ap_lb2015", replace



//-----------------
// 2016 Survey
//-----------------

clear

*Load 2016 database

use "chl_2016"

*President 

gen president="Bachelet 2"

*Year

rename numinves year

*Government approval

gen approval=.
replace approval=1 if P16STGBS==1
replace approval=0 if P16STGBS==2 | P16STGBS==-1

*Current sociotropic variable
 
gen socio_current=2 if P4STGBS==1 | P4STGBS==2
replace socio_current=1 if P4STGBS==3
replace socio_current=0 if P4STGBS==4 | P4STGBS==5

*Current egotropic variable

gen ego_current=2 if P7STGBS==1 | P7STGBS==2
replace ego_current=1 if P7STGBS==3
replace ego_current=0 if P7STGBS==4 | P7STGBS==5

keep year president approval socio_current ego_current wt


*Save 2016 database

save "ap_lb2016", replace

//-----------------
//2017 Survey
//-----------------

clear all

*Load 2017 database

use "chl_2017"

*President

gen president="Bachelet 2"

*Year
rename numinves year

*Government approval

gen approval=.
replace approval=1 if P17STGBSC==1
replace approval=0 if P17STGBSC==2 | P17STGBSC==-1

*Current sociotropic variable

gen socio_current=.
replace socio_current=2 if P4STGBSC==1|P4STGBSC==2
replace socio_current=1 if P4STGBSC==3
replace socio_current=0 if P4STGBSC==4|P4STGBSC==5

*Current egotropic variable
gen ego_current=.
replace ego_current=2 if P7STGBS==1|P7STGBS==2
replace ego_current=1 if P7STGBS==3
replace ego_current=0 if P7STGBS==4|P7STGBS==5

keep year president approval socio_current ego_current wt

*Save 2017 database

save "ap_lb2017", replace


//-----------------
//2018 Survey
//-----------------

clear all

*Load 2018 database

use "chl_2018"

*President

gen president="Piñera 2"

*Year

rename NUMINVES year

*Government approval

gen approval=.
replace approval=1 if P20STGBSC==1
replace approval=0 if P20STGBSC==2 | P20STGBSC==-1

*Rename weight variable

rename WT wt

*Current sociotropic variable

gen socio_current=.
replace socio_current=2 if P6STGBSC==1 | P6STGBSC==2
replace socio_current=1 if P6STGBSC==3
replace socio_current=0 if P6STGBSC==4 | P6STGBSC==5

*Current egotropic variable
**NOT AVAILABLE
keep socio_current year approval president wt

*Save 2018 database

save "ap_lb2018", replace

//-----------------
//2020 Survey
//-----------------
clear all

*Load 2020 database
use "chl_2020"

*President
gen president="Piñera 2"


*Year

rename numinves year

*Government approval

gen approval=.
replace approval=1 if p17stgbs==1
replace approval=0 if p17stgbs==2 | p17stgbs==-2

*Current sociotropic variable
gen socio_current=.
replace socio_current=2 if p4stgbs==1|p4stgbs==2
replace socio_current=1 if p4stgbs==3
replace socio_current=0 if p4stgbs==4|p4stgbs==5

keep socio_current year approval president wt


*Current egotropic variable
**NOT AVAILABLE

*Save 2020 database
save "ap_lb2020", replace

//----------------------------
//--------Pooled data---------
//----------------------------


clear all

*Append
use "ap_lb1995",clear
append using "ap_lb1996"
append using "ap_lb1997"
append using "ap_lb1998"
append using "ap_lb2001"
append using "ap_lb2002"
append using "ap_lb2003"
append using "ap_lb2004"
append using "ap_lb2005"
append using "ap_lb2006"
append using "ap_lb2008"
append using "ap_lb2009"
append using "ap_lb2010"
append using "ap_lb2011"
append using "ap_lb2013"
append using "ap_lb2015"
append using "ap_lb2016"
append using "ap_lb2017"
append using "ap_lb2018"
append using "ap_lb2020"

*Save 1995-2020 database
save "ap_1995-2020", replace


//-------------------------------
// Database Figure 1 and Figure 2
//-------------------------------

*Identifying people who are optimistic about the economy

gen socio_current_better=1 if socio_current==2
replace socio_current_better=0 if socio_current==1 | socio_current==0

gen ego_current_better=1 if ego_current==2
replace ego_current_better=0 if ego_current==1 | ego_current==0


*Percentages per year

collapse (mean) socio_current_better ego_current_better approval [pw=wt], by(year president) 

save "approval_data", replace
